package auth

import (
	"github.com/gin-gonic/gin"
	v1 "last/app/controllers/v1"
	adminModel "last/app/models/admin"
	"last/app/requests"
	adminRequest "last/app/requests/admin"
	"last/pkg/respose"
	"strconv"
)

type SiteController struct {
	v1.BaseController
}

// Store 新增
func (s *SiteController) Store(c *gin.Context) {
	var params = adminRequest.SiteParams{}
	err := requests.Validator(&params, c)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	var siteInfo = &adminModel.Sites{
		Avatar: params.Avatar,
		Slogan: params.Slogan,
		Name:   params.Name,
		Notice: params.Notice,
		Desc:   params.Desc,
		Domain: params.Domain,
	}
	resultBool := siteInfo.Store(siteInfo)

	if !resultBool {
		respose.Failed(c, "新增失败")
		return
	}
	respose.Success(c, "", "新增成功")
}

// List 列表
func (s *SiteController) List(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	pageSize, _ := strconv.Atoi(c.Query("page_size"))

	where := make(map[string]interface{}, 1)
	where["name"] = c.DefaultQuery("name", "")
	where["notice"] = c.DefaultQuery("notice", "")
	var siteInfo = adminModel.Sites{}
	err, total, lists := siteInfo.List(page, pageSize, where)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	result := make(map[string]interface{})
	result["total"] = total
	result["list"] = lists
	respose.Success(c, result, "获取成功")
}

// Show 详情
func (s *SiteController) Show(c *gin.Context) {
	id := c.Param("id")
	var siteInfo = adminModel.Sites{}
	err := siteInfo.Show(id, &siteInfo)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	respose.Success(c, siteInfo, "获取成功")
}

// Edit 更新
func (s *SiteController) Edit(c *gin.Context) {
	var params = adminRequest.SiteParams{}
	err := requests.Validator(&params, c)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	editData := make(map[string]interface{})
	editData["avatar"] = params.Avatar
	editData["slogan"] = params.Slogan
	editData["name"] = params.Name
	editData["notice"] = params.Notice
	editData["desc"] = params.Desc
	editData["domain"] = params.Domain
	var siteInfo = adminModel.Sites{}
	result := siteInfo.Edit(params.Id, editData)
	if result != nil {
		respose.Failed(c, "更新失败")
		return
	}
	respose.Success(c, "", "更新成功")
}

// Remove 删除
func (s *SiteController) Remove(c *gin.Context) {
	id := c.Param("id")
	var siteInfo = adminModel.Sites{}
	err := siteInfo.Remove(id, siteInfo)
	if err != nil {
		respose.Failed(c, err.Error())
		return
	}
	respose.Success(c, "", "删除成功")
}
